package de.grosjohann.android.feuerwehr;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.service.CalendarNotificationService;
import de.grosjohann.android.feuerwehr.service.FMSAuthService;
import de.grosjohann.android.feuerwehr.service.FMSNotificationService;
import de.grosjohann.android.feuerwehr.service.IntentServiceFacade;
import de.grosjohann.android.feuerwehr.service.WeatherNotificationService;
import de.grosjohann.android.feuerwehr.service.WidgetUpdateService;
import de.grosjohann.android.feuerwehr.util.FileUtil;

public class MainActivity
  extends Activity {

  private static final String NAME = "MainActivity";

  private boolean fmsStatusAuthorized = false;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btnFMS = (Button) findViewById(R.id.btnFMS);
    btnFMS.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, FMSActivity.class);
        intent.putExtra(FMSActivity.FMS_AUTHORIZED, MainActivity.this.fmsStatusAuthorized);
        startActivity(intent);
      }
    });

    Button btnWeather = (Button) findViewById(R.id.btnWeather);
    btnWeather.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, WeatherActivity.class));
      }
    });

    Button btnCalendar = (Button) findViewById(R.id.btnCalendar);
    btnCalendar.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, CalendarActivity.class));
      }
    });

    // Start FMSService
    IntentServiceFacade.restartRepeating(this, FMSNotificationService.class);

    // Start WeatherService
    IntentServiceFacade.restartRepeating(this, WeatherNotificationService.class);

    // Start CalendarService once
    IntentServiceFacade.startOnce(this, CalendarNotificationService.class);

    // Settings for FMSAuthService
    FMSAuthService.updateUIListener = new IUpdateUIListener() {
      @Override
      public void updateUI(String data) {
        try {
          JSONObject jsonObj = new JSONObject(data);
          TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
          MainActivity.this.fmsStatusAuthorized = jsonObj.getBoolean(tm.getDeviceId());
        } catch (JSONException ex) {
          Log.e(NAME, ex.getMessage(), ex);
        }
      }

      @Override
      public void showInfo(String info) {
        // nothing to do        
      }

      @Override
      public void showWarning(String warning) {
        // nothing to do        
      }

      @Override
      public void showError(String error) {
        // nothing to do        
      }
    };

    // Start FMSAuthService once
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    Intent intent = new Intent(this, FMSAuthService.class);
    intent.putExtra(FMSNotificationService.FMS_STATUS_AUTHORIZATION, tm.getDeviceId());
    startService(intent);
  }

  @Override
  protected void onResume() {
    super.onResume();
    Notify.getInstance().clear(this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case R.id.miPrefs:
        startActivity(new Intent(this, PrefsActivity.class));
        return true;
      case R.id.miInfo:
        startActivity(new Intent(this, InfoActivity.class));
        return true;
      case R.id.miUpdate:
        Notify.getInstance().clear(this);
        update();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void update() {
    FileUtil.deleteFile(this, FMSNotificationService.FMS_STATUS_DATA);
    FileUtil.deleteFile(this, WeatherNotificationService.WEATHER_DATA);
    FileUtil.deleteFile(this, CalendarNotificationService.CALENDAR_DATA);

    IntentServiceFacade.startOnce(this, FMSNotificationService.class);
    IntentServiceFacade.startOnce(this, WeatherNotificationService.class);
    IntentServiceFacade.startOnce(this, CalendarNotificationService.class);
    IntentServiceFacade.startOnce(this, WidgetUpdateService.class);
  }
}